package com.light.mytext.mycustomview

import android.R.attr.startX
import android.R.attr.startY
import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.FrameLayout
import com.light.mytext.R
import com.light.mytext.utiles.MiscUtil
import com.light.mytext.utiles.ScreenUtils
import com.light.mytext.utiles.ToastUtils
import com.light.mytext.utiles.ToolUtils
import java.lang.ref.SoftReference
import java.lang.ref.WeakReference
import kotlin.math.abs


/**
 * 光标
 */
class CurSorView(context: Context?, fatherViewWidth: Int, fatherViewHeight: Int, index: Int) :
    View(context) {
    //是否开启抗锯齿
    private val antiAlias = true
    private val mArcWidth = 1f

    //    var indexNum = 1//添加下标
    var indexNum = index//添加下标
    var mContext = context;

    //中心点
    var floatX = 0f
    var floatY = 0f
    var mPaintK: Paint? = null//默认边框
    var mPaintChoose: Paint? = null//选中边框
    var mPaintC: Paint? = null//填充
    var mPaintT: Paint? = null//文字
    var mPaintB: Paint? = null//背景
    var rect: RectF? = null//背景

    //父布局范围
    var mFatherViewWidth = fatherViewWidth
    var mFatherViewHeight = fatherViewHeight


    //圆半径
    var floatCircleRadius = 5f

    //圆心坐标，半径
    private var mCenterPoint: Point? = null

    //矩形宽高
    var floatRecHeight = 5 * floatCircleRadius
    var floatRecWidth = floatCircleRadius - 2
    var distance = 0

    //view宽高
    var mDefaultSize = 0
    var viewWidth = 0
    var viewHeight = 0

    var mCanvas: Canvas? = null

    //onTouch使用
    var nleft = 0
    var ntop = 0
    var nright = 0
    var nbottom = 0

    var downX = 0
    var downY = 0

    init {
        mDefaultSize = 60
        initData()
        initPaint()
    }

    /**
     * 计算各个距离
     */
    fun initData() {
        //根据半径获取原点距离其他点的垂直距离
        //勾股定理
        distance =
            Math.sqrt((floatCircleRadius * floatCircleRadius - (floatRecWidth / 2) * (floatRecWidth / 2)).toDouble())
                .toInt()
        mCenterPoint = Point()
    }

    @SuppressLint("ResourceType")
    fun initPaint() {
        /**
         * 默认边框
         */
        mPaintK = Paint()
        mPaintK!!.isAntiAlias = antiAlias
        // 设置画笔的样式，为FILL，FILL_OR_STROKE，或STROKE
        mPaintK!!.style = Paint.Style.STROKE
        // 设置画笔粗细
        mPaintK!!.strokeWidth = 3 * mArcWidth
        mPaintK!!.color = Color.WHITE
        mPaintK!!.textAlign = Paint.Align.CENTER
        /**
         * 选中边框
         */
        mPaintChoose = Paint()
        mPaintChoose!!.isAntiAlias = antiAlias
        // 设置画笔的样式，为FILL，FILL_OR_STROKE，或STROKE
        mPaintChoose!!.style = Paint.Style.STROKE
        // 设置画笔粗细
        mPaintChoose!!.strokeWidth = 3 * mArcWidth
        mPaintChoose!!.color = Color.TRANSPARENT
        mPaintChoose!!.textAlign = Paint.Align.CENTER
        /**
         * 填充
         */
        mPaintC = Paint()
        mPaintC!!.isAntiAlias = antiAlias
        // 设置画笔的样式，为FILL，FILL_OR_STROKE，或STROKE
        mPaintC!!.style = Paint.Style.FILL
        // 设置画笔粗细
        mPaintC!!.strokeWidth = mArcWidth
        mPaintC!!.color = Color.BLACK
        mPaintC!!.textAlign = Paint.Align.CENTER
        /**
         * 文字
         */
        mPaintT = Paint()
        mPaintT!!.isAntiAlias = antiAlias
        // 设置画笔的样式，为FILL，FILL_OR_STROKE，或STROKE
        mPaintT!!.style = Paint.Style.STROKE
        // 设置画笔粗细
        mPaintT!!.strokeWidth = mArcWidth
        mPaintT!!.color = Color.BLACK
        mPaintT!!.textSize = 15f
        mPaintT!!.textAlign = Paint.Align.CENTER
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        Log.i("AAAAAA==","onDraw")
        mCanvas = canvas
        drawKuang(mCanvas)//绘制默认边框
        drawCenter(mCanvas)//绘制填充
        drawChooseKuang(mCanvas)//绘制选中边框
        drawText(mCanvas)
    }

    /**
     * 绘制边框
     */
    fun drawKuang(canvas: Canvas?) {
        //画中心圆
        canvas!!.drawCircle(floatX, floatY, floatCircleRadius, mPaintK!!)
        //绘制左边矩形
        val pathG = Path()
        pathG.moveTo(floatX - distance, floatY - floatRecWidth / 2)//起点  暂定右上角为起点
        pathG.lineTo(floatX - distance - floatRecHeight, floatY - floatRecWidth / 2)//左上角
        pathG.lineTo(floatX - distance - floatRecHeight, floatY + floatRecWidth / 2)//左下角
        pathG.lineTo(floatX - distance, floatY + floatRecWidth / 2)//右下角
//        pathG.close() // 使这些点构成封闭的多边形
        canvas.drawPath(pathG, mPaintK!!)
        //绘制上边矩形
        pathG.reset()
        pathG.moveTo(floatX - floatRecWidth / 2, floatY - distance)//起点  暂定左下角为起点
        pathG.lineTo(floatX - floatRecWidth / 2, floatY - distance - floatRecHeight)//左上角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY - distance - floatRecHeight)//右上角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY - distance)//右下角
        canvas.drawPath(pathG, mPaintK!!)
        //绘制右边矩形
        pathG.reset()
        pathG.moveTo(floatX + distance, floatY - floatRecWidth / 2)//起点  暂定左上角为起点
        pathG.lineTo(floatX + distance + floatRecHeight, floatY - floatRecWidth / 2)//右上角
        pathG.lineTo(floatX + distance + floatRecHeight, floatY + floatRecWidth / 2)//右下角
        pathG.lineTo(floatX + distance, floatY + floatRecWidth / 2)//左下角
        canvas.drawPath(pathG, mPaintK!!)
        //绘制下边矩形
        pathG.reset()
        pathG.moveTo(floatX - floatRecWidth / 2, floatY + distance)//起点  暂定左上角为起点
        pathG.lineTo(floatX - floatRecWidth / 2, floatY + distance + floatRecHeight)//左下角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY + distance + floatRecHeight)//右下角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY + distance)//右上角
        canvas.drawPath(pathG, mPaintK!!)
    }

    /**
     * 绘制选中边框
     */
    fun drawChooseKuang(canvas: Canvas?) {
        //画中心圆
        canvas!!.drawCircle(floatX, floatY, floatCircleRadius, mPaintChoose!!)
        //绘制左边矩形
        val pathG = Path()
        pathG.moveTo(floatX - distance, floatY - floatRecWidth / 2)//起点  暂定右上角为起点
        pathG.lineTo(floatX - distance - floatRecHeight, floatY - floatRecWidth / 2)//左上角
        pathG.lineTo(floatX - distance - floatRecHeight, floatY + floatRecWidth / 2)//左下角
        pathG.lineTo(floatX - distance, floatY + floatRecWidth / 2)//右下角
//        pathG.close() // 使这些点构成封闭的多边形
        canvas.drawPath(pathG, mPaintChoose!!)
        //绘制上边矩形
        pathG.reset()
        pathG.moveTo(floatX - floatRecWidth / 2, floatY - distance)//起点  暂定左下角为起点
        pathG.lineTo(floatX - floatRecWidth / 2, floatY - distance - floatRecHeight)//左上角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY - distance - floatRecHeight)//右上角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY - distance)//右下角
        canvas.drawPath(pathG, mPaintChoose!!)
        //绘制右边矩形
        pathG.reset()
        pathG.moveTo(floatX + distance, floatY - floatRecWidth / 2)//起点  暂定左上角为起点
        pathG.lineTo(floatX + distance + floatRecHeight, floatY - floatRecWidth / 2)//右上角
        pathG.lineTo(floatX + distance + floatRecHeight, floatY + floatRecWidth / 2)//右下角
        pathG.lineTo(floatX + distance, floatY + floatRecWidth / 2)//左下角
        canvas.drawPath(pathG, mPaintChoose!!)
        //绘制下边矩形
        pathG.reset()
        pathG.moveTo(floatX - floatRecWidth / 2, floatY + distance)//起点  暂定左上角为起点
        pathG.lineTo(floatX - floatRecWidth / 2, floatY + distance + floatRecHeight)//左下角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY + distance + floatRecHeight)//右下角
        pathG.lineTo(floatX + floatRecWidth / 2, floatY + distance)//右上角
        canvas.drawPath(pathG, mPaintChoose!!)
    }

    /**
     * 取消选中
     */
    fun cancelChoose() {
        //重置选中paint
        mPaintChoose!!.reset()
        //把画笔颜色设置为透明，重绘
        mPaintChoose!!.color = Color.TRANSPARENT
        invalidate()
    }

    /**
     * 选中
     */
    fun Choose() {
        //重置选中paint
        mPaintChoose!!.reset()
        //把画笔颜色设置为透明，重绘
        mPaintChoose!!.color = resources.getColor(R.color.tv_color_00)
        invalidate()
    }

    /**
     * 绘制填充
     */
    fun drawCenter(canvas: Canvas?) {
        //画中心圆
        canvas!!.drawCircle(floatX, floatY, floatCircleRadius - mArcWidth / 2, mPaintC!!)
        //上部矩形
        canvas.drawRect(
            floatX - floatRecWidth / 2 + mArcWidth / 2,
            floatY - distance - floatRecHeight + mArcWidth / 2,
            floatX + floatRecWidth / 2 - mArcWidth / 2,
            floatY - distance,
            mPaintC!!
        )
        //下部矩形
        canvas.drawRect(
            floatX - floatRecWidth / 2 + mArcWidth / 2,
            floatY + distance,
            floatX + floatRecWidth / 2 - mArcWidth / 2,
            floatY + distance + floatRecHeight - mArcWidth / 2,
            mPaintC!!
        )
        //左边矩形
        canvas.drawRect(
            floatX - distance - floatRecHeight + mArcWidth / 2,
            floatY - floatRecWidth / 2 + mArcWidth / 2,
            floatX - distance,
            floatY + floatRecWidth / 2 - mArcWidth / 2,
            mPaintC!!
        )
        //右边矩形
        canvas.drawRect(
            floatX + distance,
            floatY - floatRecWidth / 2 + mArcWidth / 2,
            floatX + distance + floatRecHeight - mArcWidth / 2,
            floatY + floatRecWidth / 2 - mArcWidth / 2,
            mPaintC!!
        )
    }

    /**
     * 绘制下标
     */
    fun drawText(canvas: Canvas?) {
        canvas!!.drawText(
            indexNum.toString(),
            floatX + floatRecHeight / 2,
            floatY + floatRecHeight,
            mPaintT!!
        )
    }

    /**
     * rawX  rawY 触摸点距离屏幕的距离
     *   x      y 触摸点距离view边界的距离
     */
    override fun onTouchEvent(event: MotionEvent?): Boolean {


        when (event!!.action) {
            MotionEvent.ACTION_DOWN -> {
                downX = event!!.x.toInt()
                downY = event.y.toInt()
                Choose()
            }
            MotionEvent.ACTION_MOVE -> {
                //计算移动的距离
                var moveX: Int = event!!.x.toInt() - downX
                var moveY: Int = event!!.y.toInt() - downY
                var l = left + moveX
                var r = l + viewWidth
                var t = top + moveY
                var b = t + viewHeight
                //划出左边边界
                if (l < 0) {
                    l = 0
                    r = l + viewWidth
                }
                //划出右边边界
                if (r > mFatherViewWidth) {
                    r = mFatherViewWidth
                    l = r - viewWidth
                }
                //划出上边边界
                if (t < 0) {
                    t = 0
                    b = t + viewHeight
                }
                //划出下边边界
                if (b > mFatherViewHeight) {
                    b = mFatherViewHeight
                    t = b - viewHeight
                }
                layout(l, t, r, b)//错误2：这里使用了v.layout结果拖动时候异常卡顿
                Log.i(
                    "Move==",
                    "neft==" + l + "ntop==" + t + "nright==" + r + "nbottom==" + b
                )
                val params: FrameLayout.LayoutParams = layoutParams as FrameLayout.LayoutParams
                params.leftMargin=left
                params.topMargin=top
                layoutParams=params
            }
            MotionEvent.ACTION_UP -> {
                cancelChoose()
            }
        }
        return true
    }
    /**
     * 移动过快时候会出现 距离位置监听不到的情况  例如限制距离上部50  但是移动过快会在top=-6时候才监听出来  而这时候
     */

    /**
     * 以下两个方法设置View居中画布显示
     * 执行顺序（1）onMeasure（2）onSizeChanged
     */
    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        //获取圆的相关参数    w即onMeasure中设置的  设置View居中画布显示
        floatX = (w / 2).toFloat()
        floatY = (h / 2).toFloat()
        viewWidth = measuredWidth
        viewHeight = measuredHeight
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        setMeasuredDimension(
            MiscUtil.measure(widthMeasureSpec, mDefaultSize),
            MiscUtil.measure(heightMeasureSpec, mDefaultSize)
        )
    }


}
